package com.torry.replay.api.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.torry.replay.api.request.SaveExponentInfoRequest;
import com.torry.replay.api.result.DashboardStatResult;
import com.torry.replay.application.exception.ReplayExceptionEnum;
import com.torry.replay.domain.DashBoardService;
import com.torry.replay.infras.entity.DashboardDO;
import com.torry.replay.infras.repo.DashboardRepository;
import com.zen.boot.common.util.Preconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author torry.wang
 */
@RestController
@RequestMapping("/exponent")
@Api(tags = "大盘")
public class ExponentController {

    @Resource
    private DashBoardService dashBoardService;

    @Resource
    private DashboardRepository dashboardRepository;

    @PostMapping("saveExponentInfo")
    @ApiOperation(value = "保存大盘信息")
    public Boolean saveExponentInfo(@RequestBody SaveExponentInfoRequest request) {

        int count = dashboardRepository.count(new LambdaQueryWrapper<DashboardDO>().eq(DashboardDO::getDate, request.getDate()).ne(request.getId() != null, DashboardDO::getId, request.getId()));
        Preconditions.ofTrue(count == 0, ReplayExceptionEnum.DATE_REPEAT);

        return dashboardRepository.saveOrUpdate(JSON.parseObject(JSON.toJSONString(request), DashboardDO.class));
    }

    @GetMapping("getExponentInfo/{date}")
    @ApiOperation(value = "获取指定日期大盘信息")
    public DashboardDO getExponentInfo(@PathVariable String date) {
        return dashboardRepository.getOne(new LambdaQueryWrapper<DashboardDO>().eq(DashboardDO::getDate, date));
    }

    @GetMapping("limitStat/{type}/{radius}")
    @ApiOperation(value = "涨跌数据统计")
    public DashboardStatResult limitStat(@PathVariable @ApiParam(value = "统计类型 1涨跌家数统计  2涨跌停数据统计") Integer type,
                                         @PathVariable @ApiParam(value = "统计范围 1最近一年  2最近一个季度  3最近一个月") Integer radius) {

        return dashBoardService.getDashboardStatInfo(type, radius);
    }

    @GetMapping("capitalStat")
    @ApiOperation(value = "统计最近一个月大盘资金异动")
    public DashboardStatResult capitalStat() {
        return dashBoardService.getDashboardStatInfo(3, 3);
    }

    @GetMapping("volumeStat/{radius}")
    @ApiOperation(value = "大盘成交量统计")
    public DashboardStatResult volumeStat(@PathVariable @ApiParam(value = "统计范围 1最近一年  2最近一个季度  3最近一个月")Integer radius) {
        return dashBoardService.getDashboardStatInfo(4, radius);
    }
}
